package cn.demomaster.quickui_sample.ui.fragment.helper;

import android.Manifest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.widget.button.QuickButton;

import cn.demomaster.quickpermission.QuickPermission;
import cn.demomaster.quickpermission.listener.PermissionResultCallback;
import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui_sample.R;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;


/**
 * 权限管理
 * Squirrel桓
 * 2018/8/25
 */

@ActivityPager(name = "权限管理", preViewClass = TextView.class, resType = ResType.Resource)
//,iconRes = R.mipmap.ic_permission_manager)
public class PermitionFragment extends QuickFragment {

    //Components
    //@BindView(R.id.btn_write_external_storage)
    QuickButton btn_write_external_storage;
    //@BindView(R.id.btn_write_external_storage2)
    QuickButton btn_write_external_storage2;
    //@BindView(R.id.btn_group)
    QuickButton btn_group;

    @NonNull
    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup mView = (ViewGroup) inflater.inflate(R.layout.fragment_layout_permition, null);
        return mView;
    }

    public void initView(View rootView) {
        //ButterKnife.bind(this, rootView);
        btn_write_external_storage = findViewById(R.id.btn_write_external_storage);
        btn_write_external_storage2 = findViewById(R.id.btn_write_external_storage2);
        btn_group = findViewById(R.id.btn_group);
        getActionBarTool().setTitle("动态权限");
        btn_write_external_storage.setOnClickListener(v -> {QuickPermission.with(getActivity()).permissions(new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE})
                .onGranted(new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        return null;
                    }
                }).request();
        });
        btn_write_external_storage2.setOnClickListener(v -> {
                    QuickPermission.with(mContext).permissions(
                                    new String[]{
                                            Manifest.permission.READ_EXTERNAL_STORAGE,
                                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                            Manifest.permission.WRITE_SETTINGS,
                                            Manifest.permission.SYSTEM_ALERT_WINDOW})
                            .onGranted(new Function0<Unit>() {
                                @Override
                                public Unit invoke() {
                                    Toast.makeText(getActivity(), "all通过", Toast.LENGTH_LONG).show();
                                    return null;
                                }
                            }).request();
                });

        btn_group.setOnClickListener(v -> {
            QuickPermission.with(getActivity()).permissions(
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.READ_PHONE_STATE})
                    .onGranted(new Function0<Unit>() {
                        @Override
                        public Unit invoke() {
                            Toast.makeText(getActivity(), "all通过", Toast.LENGTH_LONG).show();
                            return null;
                        }
                    });
        });
    }


    /*public void initActionBarLayout(ActionBarLayout2 actionBarLayoutOld) {
        int i = (int) (Math.random() * 10 % 4);
        actionBarLayoutOld.setTitle("audio play");
        actionBarLayoutOld.setHeaderBackgroundColor(Color.RED);

    }*/
}